/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Anthares
 */
@Entity
@Table(name = "CATEG_PROD")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CategProd.findAll", query = "SELECT c FROM CategProd c"),
    @NamedQuery(name = "CategProd.findByCategId", query = "SELECT c FROM CategProd c WHERE c.categId = :categId"),
    @NamedQuery(name = "CategProd.findByLibelle", query = "SELECT c FROM CategProd c WHERE c.libelle = :libelle")})
public class CategProd implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CATEG_ID", nullable = false)
    private Integer categId;

    @Basic(optional = false)
    @Size(max = 255)
    @Column(length = 255)
    private String libelle;

    @JoinTable(name = "PROMO_CATEG", joinColumns = {
        @JoinColumn(name = "CATEG_ID", referencedColumnName = "CATEG_ID", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "PROMO_ID", referencedColumnName = "PROMO_ID", nullable = false)})
    @ManyToMany
    private List<Promotions> promotionsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "categId")
    private List<Produit> produitList;

    @Transient
    private TradCateg traduction;
    
    @Transient
    private Promotions bestPromo;

    public Promotions getBestPromo() {
        return bestPromo;
    }

    public void setBestPromo(Promotions bestPromo) {
        this.bestPromo = bestPromo;
    }

    public CategProd() {
    }

    public CategProd(Integer categId) {
        this.categId = categId;
    }

    public Integer getCategId() {
        return categId;
    }

    public void setCategId(Integer categId) {
        this.categId = categId;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public TradCateg getTraduction() {
        return traduction;
    }

    public void setTraduction(TradCateg traduction) {
        this.traduction = traduction;
    }

    @XmlTransient
    public List<Promotions> getPromotionsList() {
        return promotionsList;
    }

    public void setPromotionsList(List<Promotions> promotionsList) {
        this.promotionsList = promotionsList;
    }

    @XmlTransient
    public List<Produit> getProduitList() {
        return produitList;
    }

    public void setProduitList(List<Produit> produitList) {
        this.produitList = produitList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (categId != null ? categId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if(!(object instanceof CategProd)){
            return false;
        }
        CategProd other = (CategProd) object;
        if((this.categId == null && other.categId != null) || (this.categId != null && !this.categId.equals(other.categId))){
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Model.CategProd[ categId=" + categId + " ]";
    }
    
    public boolean isOnPromotion(){
        return this.bestPromo!=null;
    }
}
